dojo.declare("MCustomerOrderSelectPg", wm.Page, {

    start: function() {

    },
    "preferredDevice": "phone",

    isNumber: function(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    },
    selectOrderItem: function(selItemId, itemRowNum) {

        var itemExist = false;
        var data = this.vOrderedItems.getData();

        for (var i = 0; i < data.length; i++) {
            var itemSel = data[i].itemId;
            if (itemSel === selItemId) {
                itemExist = true;
                this.vOrderedItems.removeItem(i);
            }

        }

        if (!itemExist) {

            var rowData = this.listVendorItems.getItemData(itemRowNum);

            var price = (1 * rowData.price);

            this.vOrderedItems.addItem({
                itemId: selItemId,
                itemName: rowData.itemName,
                quantity: 1,
                orderedPrice: price,
                desc: rowData.itemDesc
            });

        }

        //
    },

    selectItemQuantity: function(selItemId, selIndex) {


        var data = this.vOrderedItems.getData();
        var newItemName = '';
        var newDesc = '';
        var newItemPrice = 1;
        var qty = selIndex + 1;
        var price = 1;
        for (var i = 0; i < data.length; i++) {
            var itemSel = data[i].itemId;
            if (itemSel === selItemId) {
                newItemName = data[i].itemName;
                newDesc = data[i].desc;

                newItemPrice = data[i].orderedPrice;
                price = (qty * data[i].orderedPrice);

                this.vOrderedItems.removeItem(i);
            }

        }


        this.vOrderedItems.addItem({
            itemId: selItemId,
            itemName: newItemName,
            quantity: qty,
            orderedPrice: price,
            desc: newDesc
        });

    },

    listVendorItemsItemNameFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {

        // var items = '<div class="MobileRow" style="font-size:12px">Quantity </div> <div class="MobileRow"> <select name="Quantity" onchange="main.pcCustomerContent.page.pcItemList.page.selectItemQuantity(' + rowObj.itemId + ',this.selectedIndex);"> ' + '<option value="1">1</option>' + '<option value="2">2</option>' + '<option value="3">3</option>' + '<option value="4">4</option>' + '<option value="5">5</option>' + '<option value="6">6</option>' + '<option value="7">7</option>' +
        //
        // '</select> </div>';
        var display = "<div class='MobileRow' style='font-size:13px'><b>" + rowObj.itemName + "</b></div>" + "<div class='MobileRow'> </div>" + "<div class='MobileRow'>Price: " + rowObj.price + "</div>";

        return display;
    },
    listVendorItemsItemIdFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var selItemName = rowObj.itemName;
        //alert(selItemName);
        //    return '<INPUT TYPE=CHECKBOX NAME="defaultView" CHECKED onchange=main.pcContent.page.pcDashboard.page.pdlgViewPreferenceOptions.page.pdManagePreference.page.setDefaultUserPreference(' + inValue + ',' + rowId + ')><P>';
        //var chkBoxStr = '<div class="MobileRow">Order</div>' + ' <INPUT TYPE="CHECKBOX" NAME=chk' + inValue + '" id="chk' + inValue + '" onchange="main.pcCustomerContent.page.pcItemList.page.selectOrderItem('+rowId+ ',' + inValue + ');" ></input>';
        var chkBoxStr = '<div class="MobileRow" style="font-size:13px">Order</div> <div class="MobileRow" style="font-size:13px"> <INPUT TYPE=BUTTON height="20px" NAME="defaultView" onchange=mobileMainPg.pcContent.page.pcItemList.page.selectOrderItem(' + inValue + ',' + rowId + ') > </div>';
        // alert(chkBoxStr);
        return chkBoxStr;
    },
    listVendorItemsQuantityFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {

        var items = '<div class="MobileRow" style="font-size:12px">Qty </div> <div class="MobileRow"> <select name="Quantity" onchange="mobileMainPg.pcContent.page.pcItemList.page.selectItemQuantity(' + rowObj.itemId + ',this.selectedIndex);"> ' + '<option value="1">1</option>' + '<option value="2">2</option>' + '<option value="3">3</option>' + '<option value="4">4</option>' + '<option value="5">5</option>' + '<option value="6">6</option>' + '<option value="7">7</option>' +

        '</select> </div>';
        return items;
    },
    listVendorItemsItemDescFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {

        var commentBox = "<div class='MobileRow'>Comment:</div>" + "<input type='text' name='comment'/>";

        return commentBox;
    },

    list1ItemIdFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var display = "<div class='MobileRowTitle'>Item Name: " + rowObj.desc + "</div>" + "<div class='MobileRow'>Quantity: " + rowObj.quantity + "</div>" + "<div class='MobileRow'>OrderedPrice: " + rowObj.orderedPrice + "</div>";


        return display;
    },
    svPlaceOrderSuccess: function(inSender, inDeprecated) {

    },
    listVendorItemsImageUrlFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        try {
            var file = "services/FileUploadDownload.download?method=downloadFile&fileName=" + inValue;
            var imgUrl = "http://www.mariasmenu.com/wp-content/uploads/Prawns-biriyani.jpg";
            return '<img  style="height: 60px;width: 60px;" src="' + imgUrl + '" />';
        } catch (e) {
            alert(e);
            console.error('ERROR IN dojoGrid2PicurlFormat: ' + e);
        }
    },

    listFinalOrderItemNameFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var display = "<div class='MobileRowTitle'>Item Name: " + rowObj.itemName + "</div>" + "<div class='MobileRowTitle'>Item Desc: " + rowObj.desc + "</div>" + "<div class='MobileRow'>Quantity: " + rowObj.quantity + "</div>" + "<div class='MobileRow'>OrderedPrice: " + rowObj.orderedPrice + "</div>";
        return display;
    },

    listVendorItemsPriceFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var display = "<div class='MobileRow' style='font-size:13px'><font color='red'>Price </font></div>  <div class='MobileRow' style='font-size:13px'><b> <font color='red'>" + rowObj.price + " Rs</font></b></div>";

        return display;

    },
    lvVendorItemsSuccess: function(inSender, inDeprecated) {


        //wmlist-row
        if (this.getValueById("mainPanel")) {
            this.getValueById("mainPanel").destroy();

        }
       
        var parentPanel = this.panelVendorItems;

        var props = eval('({layoutKind:"top-to-bottom",horizontalAlign:"left",id:"mainPanel",width: "100%",height: "100%"})');
        parentPanel.createComponent("mainPanel", "wm.Panel", props);
        var mainPanel = this.getValueById("mainPanel");
       // mainPanel.setBorder("1");
        mainPanel.setMargin("5");
        mainPanel.autoScroll = true;
        mainPanel.setAutoScroll(true);
        mainPanel.setScrollY(true);
        mainPanel.setBorderColor("#333333");
        if (mainPanel.color) {
            mainPanel.color = "#FFFFFF";

        }
        if (mainPanel.styles.color) {
           
            mainPanel.styles.color = "#FFFFFF";
        }

        //parentWidget.createComponent(inName, inType, inProps, inEvents, inChildren, inOwner);
        for (var count = 0; count < this.lvVendorItems.getCount(); count++) {

            var vItem = this.lvVendorItems.getItem(count);

            //panel,picture
            var panelId = "panel" + vItem.data.itemId;

            props = eval('({layoutKind:"left-to-right",horizontalAlign:"left",verticalAlign:"center",id:"' + panelId + '",width: "100%",height: "90px"})');
            mainPanel.createComponent(panelId, "wm.Panel", props);
            var panelRow = this.getValueById(panelId);
            panelRow.setBorder("1");
            panelRow.setBorderColor("#333333");
            panelRow.setMargin("3");
            // panelRow.setBackgroundColor("#333333");
            var pictureId = "picture" + vItem.data.itemId;
            //wm.xhrPath = 'http://192.168.1.6:8094/servicelive/'
            var appPath = wm.xhrPath + "services/FileUploadDownload.download?method=downloadFile&fileName=" + vItem.data.imageUrl;
            props = eval('({aspect:"h",id:"' + pictureId + '",width: "70px",height: "70px",source :"' + appPath + '"})');
            panelRow.createComponent(pictureId, "wm.Picture", props);
            var picture = this.getValueById(pictureId);

            var labelPanelId = "labelPanel" + vItem.data.itemId;

            props = eval('({layoutKind:"top-to-bottom",horizontalAlign:"left",verticalAlign:"top",id:"' + labelPanelId + '",width: "100%",height: "100%"})');
            panelRow.createComponent(labelPanelId, "wm.Panel", props);
            var labelPanel = this.getValueById(labelPanelId);


            var labelNameId = "labelName" + vItem.data.itemId;
            var displayLabel = "<div class='MobileRow' style='font-size:13px'><b>" + vItem.data.itemName + "</b> </br> Price:</div>"; //+ "<div class='MobileRow'> </div>" + "<div class='MobileRow'>Price: " + vItem.data.price + "</div>";
            props = eval('({caption:"' + displayLabel + '",id:"' + labelNameId + '",width: "100%",height: "100%",captionPosition:"left",captionAlign:"left",captionSize :"40px"})');
            labelPanel.createComponent(labelNameId, "wm.Label", props);
            var labelName = this.getValueById(labelNameId);


            var labelPriceId = "labelPrice" + vItem.data.itemId;
            displayLabel = "<div class='MobileRow'> </div>" + "<div class='MobileRow'>Price: " + vItem.data.price + "</div>";

            props = eval('({caption:"' + displayLabel + '",id:"' + labelPriceId + '",width: "100%",height: "100%",captionPosition:"left",captionAlign:"left",captionSize :"40px"})');
            labelPanel.createComponent(labelPriceId, "wm.Label", props);
            var labelPrice = this.getValueById(labelPriceId);


//////////////////////////// quantity
            var orderQtyPanelId = "orderQtyPanel" + vItem.data.itemId;

            props = eval('({layoutKind:"top-to-bottom",horizontalAlign:"left",verticalAlign:"top",id:"' + orderQtyPanelId + '",width: "60px",height: "100%"})');
            panelRow.createComponent(orderQtyPanelId, "wm.Panel", props);
            var panelQty = this.getValueById(orderQtyPanelId);

            var optionId = "qtySelect" + vItem.data.itemId;

            props = eval('({caption:"Quantity",id:"' + optionId + '",options:"' + app.vQuantity + '",captionPosition: "top",captionSize: "20px",height: "75%",width: "100%"})');
            panelQty.createComponent(optionId, "wm.SelectMenu", props);

            var optionQty = this.getValueById(optionId); // get the component by id
            optionQty.setDataSet(app.vQuantity);
            optionQty.setDataValue(1);
            optionQty.connect(optionQty, "onselect", dojo.hitch(this, "selectQty", optionQty));



////////////////////////////order panel


            var orderPanelId = "orderSelectPanel" + vItem.data.itemId;

            props = eval('({layoutKind:"top-to-bottom",horizontalAlign:"left",verticalAlign:"middle",id:"' + orderPanelId + '",width: "55px",height: "100%"})');
            panelRow.createComponent(orderPanelId, "wm.Panel", props);
            var panelOrderQty = this.getValueById(orderPanelId);
            ////////////////////////////////////////////////////////////////////
            var chkBoxImg = "chkBoxImg_" + vItem.data.itemId;
            //wm.xhrPath = 'http://192.168.1.6:8094/servicelive/'
            var chkBoxImgPath = "resources/images/checkbox.jpg";
            props = eval('({aspect:"h",id:"' + chkBoxImg + '",width: "50px",height: "50px",source :"' + chkBoxImgPath + '"})');
            panelOrderQty.createComponent(chkBoxImg, "wm.Picture", props);
            var chkBoxImgComp = this.getValueById(chkBoxImg);
            chkBoxImgComp.setShowing(true);
            chkBoxImgComp.connect(chkBoxImgComp, "onclick", dojo.hitch(this, "showChecked", chkBoxImgComp));

            //checked
            var chkBoxImgChecked = "chkBoxImgChecked_" + vItem.data.itemId;
            //wm.xhrPath = 'http://192.168.1.6:8094/servicelive/'
            var chkBoxImgPathChecked = "resources/images/checkboxchecked.jpg";
            props = eval('({aspect:"h",id:"' + chkBoxImgChecked + '",width: "50px",height: "50px",source :"' + chkBoxImgPathChecked + '"})');
            panelOrderQty.createComponent(chkBoxImgChecked, "wm.Picture", props);
            var chkBoxImgCompChecked = this.getValueById(chkBoxImgChecked);
            chkBoxImgCompChecked.setShowing(false);
            chkBoxImgCompChecked.connect(chkBoxImgCompChecked, "onclick", dojo.hitch(this, "showUnchecked", chkBoxImgCompChecked));


            ///////////////////////////////////////////////////////////////////////
            /**  var chkId = "chk" + vItem.data.itemId;
             
             props = eval('({caption:"Order",id:"' + chkId + '", dataType:"boolean",width: "100%",height: "100%",captionPosition:"left",captionAlign:"left",captionSize :"40px"})');
             panelOrderQty.createComponent(chkId, "wm.Checkbox", props);
             
             var chkBox = this.getValueById(chkId); // get the component by id
             chkBox.checkedValue = vItem.data.itemId;
             
             chkBox.connect(chkBox, "onchange", dojo.hitch(this, "selectOrder", chkBox));
             **/



        }
        mainPanel.reflow();

    },
    selectOrder: function(inSender) {
        console.error(inSender);

    },
    showUnchecked: function(inSender) {
        var clickedPctId = inSender.id;
        var itemId = clickedPctId.substr(clickedPctId.indexOf('_') + 1);

        inSender.setShowing(false);
        var chkPicture = this.getValueById('chkBoxImg_' + itemId);
        chkPicture.setShowing(true);


    },
    showChecked: function(inSender) {

        var clickedPctId = inSender.id;
        var itemId = clickedPctId.substr(clickedPctId.indexOf('_') + 1);
        inSender.setShowing(false);
        var chkPicture = this.getValueById('chkBoxImgChecked_' + itemId);
        chkPicture.setShowing(true);

    },
    selectQty: function(inSender) {

        console.error(inSender);
    },
    bOrderPreviewClick1: function(inSender) {

        this.vOrderedItems.clearData();
        var totalPrice = 0;

        for (var count = 0; count < this.lvVendorItems.getCount(); count++) {

            var vItem = this.lvVendorItems.getItem(count);

            var chkId = "chkBoxImgChecked_" + vItem.data.itemId;
            var chkBox = this.getValueById(chkId);

            if (chkBox.showing) {

                var optionId = "qtySelect" + vItem.data.itemId;
                var optionQty = this.getValueById(optionId);

                var qty = optionQty.getDataValue();
                var price = (qty * vItem.data.price);
                totalPrice = totalPrice + price;
                this.vOrderedItems.addItem({
                    itemId: vItem.data.itemId,
                    itemName: vItem.data.itemName,
                    quantity: optionQty.getDataValue(),
                    orderedPrice: price,
                    desc: vItem.data.itemDesc
                });

            }
        }
        if (this.vOrderedItems.getCount() > 0) {
            this.labelSelectItem.setShowing(false);
            this.labelTotalOrderPrice.setCaption("Total Price:" + totalPrice);
            this.buttonConfirmOrder.setShowing(true);
            this.labelReviewOrder.setShowing(true);


        } else {
            this.labelReviewOrder.setShowing(false);
            this.labelSelectItem.setShowing(true);
            this.labelTotalOrderPrice.setShowing(false);
            this.buttonConfirmOrder.setShowing(false);

        }

    },
    buttonAnotherOrderClick: function(inSender) {

        wm.Page.getPage("MCustomerHomePg").ncServices.update();

    },

    buttonConfirmOrderClick: function(inSender) {
        var mainPage = wm.Page.getPage("MobileMainPg");

        this.svCompleteOrder.input.setValue("order.customerId", mainPage.vUserId);

        this.svCompleteOrder.update();

    },
    svCompleteOrderSuccess1: function(inSender, inDeprecated) {

        this.vOrderedItems.clearData();
        this.lvVendorItems.update();
        // this.ncDisplayOrderResult.update();
        this.ncSelectOrder.update();
        this.panelOrderResult.setShowing(true);
        this.labelOrderResult.setCaption(inSender.data.dataValue);



    },
    _end: 0
});